﻿using System;
using System.Xml.Linq;
using System.Collections.Generic;
using ClrExtensions.Net.Rest;
using MediaPortal.GUI.Library;

namespace MyNetflix2
{
    class RentalHistory : Queue
    {
        private User _user = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="user"></param>
        public RentalHistory(User user)
            : base(user)
        { _user = user; }

        /// <summary>
        /// Request a resource
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public override List<QueueItem> Refresh(int iSelectedItem)
        {
            RestCall request;

            //--------------------------------------------------------------------------------
            // First figure out which History Request this is... It will default to All
            //--------------------------------------------------------------------------------
            switch (iSelectedItem)
            {
                case 1:
                    request = RestCalls.GetRentalHistory(base.NetFlixUser.Token, StartIndex, EndIndex);
                    break;
                case 2:
                    request = RestCalls.GetRentalHistoryShipped(base.NetFlixUser.Token, StartIndex, EndIndex);
                    break;
                case 3:
                    request = RestCalls.GetRentalHistoryReturned(base.NetFlixUser.Token, StartIndex, EndIndex);
                    break;
                case 4:
                    request = RestCalls.GetRentalHistoryWatched(base.NetFlixUser.Token, StartIndex, EndIndex);
                    break;
                default:
                    request = RestCalls.GetRentalHistory(base.NetFlixUser.Token, StartIndex, EndIndex);
                    break;
            }

            try
            {
                //--------------------------------------------------------------------------------
                // Make the call to Netflix to get the History Queue
                //--------------------------------------------------------------------------------
                string xml = base.NetFlixUser.NetflixConnection.RequestResource(request, base.NetFlixUser.Token);

                //--------------------------------------------------------------------------------
                // Parse with Linq
                //--------------------------------------------------------------------------------
                XElement root = XElement.Parse(xml);

                //--------------------------------------------------------------------------------
                // Store the elements in the QueueItems class
                //--------------------------------------------------------------------------------
                foreach (XElement node in root.Elements("rental_history_item"))
                {
                    QueueItems.Add(new QueueItem(node, _user));
                }

                return QueueItems;
            }
            catch (Exception ex)
            {
                Log.Error("My NetFlix - RentalHistory:Refresh(): Error - " + ex.Message);
                return null;
            }
        }
    }
}
